package com.burgess.net;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 测试消息丢失内容的配置
 * 创建人    BurgessLee
 * 创建时间   2020/5/30
 * 描述
 */
public class Receiver {

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.31.150:61617");
        Connection conn = connFactory.createConnection();
        conn.start();
        Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        try {
            Queue queue = session.createQueue("my-queue02");

            MessageConsumer consumer = session.createConsumer(queue);

            for (int i = 0; i < 30; i++) {
                Message message = consumer.receive();
                String jmsType = message.getJMSType();
                System.out.println("消息类型:"+jmsType);
                TextMessage tx=(TextMessage)message;
                session.commit();
                System.out.println("收到消息:"+tx.getText());
                //每个接收一次消息睡眠1秒
                Thread.sleep(2000);
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            session.close();
            conn.close();
        }
    }

}
